// pages/add-address/index.js
import area from '../../utils/area'
const api = require('../../utils/request')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    areaList:{},
    showArea: false,
    name:'',
    tel:'',
    addressDetail:'',
    addressProvince:'',
    pros:[],
    checked: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.loadAddress(options.id)
    }
    this.setData({
      areaList:area
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  areaConfirmHandle(e){
    // e.detail.index 为索引。 e.detail.values 对应的值
    const p = e.detail.values.reduce((last,cur,index)=>{
      if(index==0){
        return last+cur.name
      }else{
        return last+' / '+ cur.name
      }
    },'')
    this.setData({
      pros: e.detail.values,
      addressProvince: p,
      showArea: false
    })
  },
  showAreaHandle(){
    this.setData({
      showArea: true
    })
    
  },
  areaHideHnadle(){
    this.setData({
      showArea: false
    })
  },
  inputName(e){
    this.setData({
      name: e.detail
    })
  },
  inputTel(e){
    this.setData({
      tel: e.detail
    })
  },
  inputDetail(e){
    this.setData({
      addressDetail: e.detail
    })
  },
  changeAddressDeault(e){
    this.setData({
      checked: e.detail
    })
  },
  submitAddress(){
    if(!this.data.name){
      wx.showToast({
        title: '姓名不能为空',
        icon:'none'
      })
      return
    }
    if(this.data.tel.length!==11){
      wx.showToast({
        title: '请输入11为手机号',
        icon:'none'
      })
      return
    }
    if(/^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5,6,7]|17[0-8]|18[0-9]|19[0-3,5-9])\\d{8}$/.test(this.data.tel)){
      wx.showToast({
        title: '请输入正取的手机号',
        icon:'none'
      })
      return
    }
    if(this.data.pros.length===0){
      wx.showToast({
        title: '请选择省市',
        icon:'none'
      })
      return
    }
    if(this.data.addressDetail.length<6){
      wx.showToast({
        title: '详细地址不少于6个字',
        icon:'none'
      })
      return
    }
    if(this.data.id){
      wx.showLoading({
        title: '正在更新地址',
      })
      api.post('/user/address/save',{
        id: this.data.id,
        addressDetail: this.data.addressDetail,
        isDefault: this.data.checked,
        name: this.data.name,
        tel: this.data.tel,
        areaCode: this.data.pros[2].code,
        postCode: '',
        province: this.data.pros[0].name,
        city: this.data.pros[1].name,
        district: this.data.pros[2].name
      }).then(res=>{
        wx.hideLoading();
        wx.showToast({
          title: '地址更新成功',
          duration: 3000,
          success:()=>{
            wx.navigateBack()
          }
        })
      })
    }else{    
      wx.showLoading({
        title: '正在添加地址中',
      })
      api.post('/user/address/save',{
        addressDetail: this.data.addressDetail,
        isDefault: this.data.checked,
        name: this.data.name,
        tel: this.data.tel,
        areaCode: this.data.pros[2].code,
        postCode: '',
        province: this.data.pros[0].name,
        city: this.data.pros[1].name,
        district: this.data.pros[2].name
      }).then(res=>{
        wx.hideLoading();
        wx.showToast({
          title: '地址添加成功',
          icon: 'success',
          duration: 3000
        })
        setTimeout(()=>{
          wx.navigateBack()
        },3000);
      })
    }
  },
  loadAddress(id){
    api.get(`/user/address/${id}`).then(res=>{
      this.setData({
        id:id,
        name: res.name,
        tel: res.tel,
        addressProvince: res.province+' / '+res.city+' / '+res.district,
        addressDetail: res.addressDetail,
        checked: res.isDefault,
        pros: [
          {
            name:res.province
          },
          {
            name:res.city
          },
          {
            code: res.areaCode,
            name:res.district
          }
        ]
      })
    })
  },
  delAddress(){
    Dialog.confirm({
      title: '提示',
      message: '您真的要删除该地址吗？',
    }).then(() => {
      wx.showLoading({
      })
      debugger
      api.del(`/user/address/${this.data.id}`).then(()=>{
        wx.hideLoading()
        wx.showToast({
          title: '删除成功',
          icon:'success',
          duration: 3000,
        })
        setTimeout(()=>{
          debugger
          wx.navigateBack()
        },3000);
      })
    }).catch(() => {
      // on cancel
    });
  }
})